//
//  commentDetailsView.m
//  Streaming
//
//  Created by 官琛 on 15-6-27.
//  Copyright (c) 2015年 liuxun. All rights reserved.
//

#import "commentDetailsView.h"

@implementation commentDetailsView

-(void)dealloc
{
    [_webView release];
    [_title1 release];
    [_nameLabel release];
    [_label release];
    [super dealloc];
    
}


-(instancetype)initWithFrame:(CGRect)frame comID:(NSInteger)comId
{
    self = [super initWithFrame:frame];
    if (self) {
        self.comId = comId;
        if (WIDTH == 320 && HEIGHT == 568) {
            [self creatView5];
        }
        if (WIDTH == 414 && HEIGHT == 736) {
            [self creatViewPlus];
        }
        if (WIDTH == 375 && HEIGHT == 667){
            [self creatView6];
        }

        
        [self afnGetRequest];
    }
    return self;
}

-(void)creatView5
{
    
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(5, 64, WIDTH-10, HEIGHT-110)];
    self.webView.backgroundColor = [UIColor whiteColor];
    self.webView.delegate = self;
    [self.webView setScalesPageToFit:YES];
    [self addSubview:self.webView];
    
    [_webView autorelease];
    
    
    self.title1 = [[UILabel alloc]initWithFrame:CGRectMake(0, -70, WIDTH - 10, 60)];
//        self.title1.backgroundColor = [UIColor cyanColor];
    //    [self.webView.scrollView addSubview:self.title1];
    self.title1.numberOfLines = 0;
    self.title1.font = [UIFont boldSystemFontOfSize:20];
    [_title1 autorelease];
    
    self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, self.title1.frame.origin.y + self.title1.frame.size.height - 8, WIDTH, 20)];
    self.nameLabel.font = [UIFont systemFontOfSize:10];
    //    self.nameLabel.backgroundColor = [UIColor cyanColor];
    //    [self.webView.scrollView addSubview:self.nameLabel];
    [_nameLabel autorelease];
    
    
    
    
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(230, self.title1.frame.origin.y + self.title1.frame.size.height-8, WIDTH, 20)];
    self.label.font = [UIFont systemFontOfSize:10];
    //    self.label.backgroundColor = [UIColor cyanColor];
    //    [self.webView.scrollView addSubview:self.label];
    [_label autorelease];
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.webView animated:YES];
    self.HUD.labelText = @"正在加载中";
    [self.HUD show:YES];
    [_HUD autorelease];
    
    
}


-(void)creatView6
{
    
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(10, 70, WIDTH-15, HEIGHT-110)];
    self.webView.backgroundColor = [UIColor whiteColor];
    self.webView.delegate = self;
    [self.webView setScalesPageToFit:YES];
    [self addSubview:self.webView];
    
    [_webView autorelease];
    
    
    self.title1 = [[UILabel alloc]initWithFrame:CGRectMake(5, -70, WIDTH - 10, 60)];
    self.title1.numberOfLines = 0;
//    self.title1.backgroundColor = [UIColor cyanColor];
//    [self.webView.scrollView addSubview:self.title1];
    self.title1.font = [UIFont boldSystemFontOfSize:20];
    [_title1 autorelease];
    
    self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, self.title1.frame.origin.y + self.title1.frame.size.height - 5, WIDTH, 20)];
    self.nameLabel.font = [UIFont systemFontOfSize:10];
//    self.nameLabel.backgroundColor = [UIColor cyanColor];
//    [self.webView.scrollView addSubview:self.nameLabel];
    [_nameLabel autorelease];
    
   
    
    
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(290, self.title1.frame.origin.y + self.title1.frame.size.height- 5, WIDTH, 20)];
    self.label.font = [UIFont systemFontOfSize:10];
//    self.label.backgroundColor = [UIColor cyanColor];
//    [self.webView.scrollView addSubview:self.label];
    [_label autorelease];
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.webView animated:YES];
    self.HUD.labelText = @"正在加载中";
    [self.HUD show:YES];
    [_HUD autorelease];

        
}
-(void)creatViewPlus
{
    
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(10, 70, WIDTH-10, HEIGHT-110)];
    self.webView.backgroundColor = [UIColor whiteColor];
    self.webView.delegate = self;
    [self.webView setScalesPageToFit:YES];
    [self addSubview:self.webView];
    [_webView autorelease];
    
    
    self.title1 = [[UILabel alloc]initWithFrame:CGRectMake(0, -70, WIDTH-10, 60)];
    self.title1.numberOfLines = 0;
    //    self.title1.backgroundColor = [UIColor cyanColor];
    //    [self.webView.scrollView addSubview:self.title1];
    self.title1.font = [UIFont boldSystemFontOfSize:20];
    [_title1 autorelease];
    
    self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, self.title1.frame.origin.y+self.title1.frame.size.height-5, WIDTH, 20)];
    self.nameLabel.font = [UIFont systemFontOfSize:10];
    //    self.nameLabel.backgroundColor = [UIColor cyanColor];
    //    [self.webView.scrollView addSubview:self.nameLabel];
    [_nameLabel autorelease];
    
    
    
    
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(320, self.nameLabel.frame.origin.y, 100, 20)];
    self.label.font = [UIFont systemFontOfSize:10];
    //    self.label.backgroundColor = [UIColor cyanColor];
    //    [self.webView.scrollView addSubview:self.label];
    [_label autorelease];
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.webView animated:YES];
    self.HUD.labelText = @"正在加载中";
    [self.HUD show:YES];
    [_HUD autorelease];
    
    
}

-(void)afnGetRequest
{
    //详情http://api.m.mtime.cn/Review/Detail.api?reviewId=7902865
    //http://api.m.mtime.cn/MobileMovie/Review.api?needTop=false
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = [NSString stringWithFormat:@"http://api.m.mtime.cn/Review/Detail.api?reviewId=%ld",self.comId];
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
    
    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
//                    NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
       
        
     
            self.title1.text = [responseObject objectForKey:@"title"];
            //        self.webView = [responseObject objectForKey:@"content"];
            [self.webView loadHTMLString:[responseObject objectForKey:@"content"] baseURL:nil];
            self.nameLabel.text = [responseObject objectForKey:@"time"];
            self.label.text = [NSString stringWithFormat:@"来自:%@", [responseObject objectForKey:@"nickname"]];
        
        //        NSLog(@"%ld",self.array.count);
        
        
        [self.webView.scrollView addSubview:self.title1];
        [self.webView.scrollView addSubview:self.nameLabel];
        [self.webView.scrollView addSubview:self.label];
        [self.HUD hide:YES];
        
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
                [self.HUD hide:YES];
        
        
        
    }];
    
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '260%'"];
    
    //字体颜色
    //    [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'gray'"];
    //    //页面背景色
    //    [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#2E2E2E'"];
    
    
}

@end
